//index.js
//获取应用实例
const app = getApp();
Page({
  data: {
    banner: '',
    barSelected: 0,
    // 类型
    btype: 0,
    // 排序
    ptype: 0,
    typeList: [],

    pages: 0,
    goodsList: []
  },

  onLoad: function (options) {
    var that = this;
    this.setData({
      pages: 0,
      goodsList: []
    })
    this.getGoodsList();
    this.getTypeList();
    options.pageName && (wx.setNavigationBarTitle({
      title: options.pageName,
    }),this.setData({shareAppMessageTitle: options.pageName}))
  },

  // 获取产品类型列表
  getTypeList() {
    var that = this;
    var data = {
      point_url: app.server.agentApi.getGoodsTypeList
    };
    app.server.getConnect(data).then(function (res) {
      that.setData({
        typeList: res.data,
        banner: res.banner
      })
    })
  },

  onShow: function () {
    // this.setData({
    //   pages: 0,
    //   goodsList: []
    // })
    // this.getGoodsList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.pageOnloadInit(this, app);
  },

  onReachBottom: function () {
    this.getGoodsList();
  },

  getGoodsList: function () {
    var that = this;
    this.data.pages++;
    var data = {
      point_url: app.server.agentApi.goodsList,
      type: this.data.btype == 0 ? '' : this.data.btype,
      pageNumber: this.data.pages,
      pageSize: 10,
      sortOrder: this.data.ptype == 0 ? '' : this.data.ptype == 1 ? 'asc' : 'desc'
    };
    app.server.getConnect(data).then(function (res) {
      that.setData({
        goodsList: that.data.goodsList.concat(res.data.rows || [])
      })
    })
  },

  toBusinessDetails: function (e) {
    wx.navigateTo({
      url: '/pages/agent/plugins/business/businessDetails/businessDetails?id=' + e.currentTarget.dataset.id,
    })
  },

  // 选择类型
  bindPickerChange: function (e) {
    this.setData({
      barSelected: 0,
      btype: this.data.typeList[e.detail.value]['id'],
      btypeTxt: this.data.typeList[e.detail.value]['name'],
      ptype: 0,
      pages: 0,
      goodsList: []
    })
    this.getGoodsList();
  },

  barClick: function () {
    var that = this;
    this.setData({
      barSelected: 1,
      ptype: that.data.ptype === 0 ? 2 : (that.data.ptype === 1 ? 2 : 1),
      pages: 0,
      goodsList: []
    })
    this.getGoodsList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
